'use strict';
/**
 * @Controller 顾客模块
 */
const Controller = require('../base');

class SysUserController extends Controller {
  /* -----------------增----------------- */
  /* 添加新顾客 */

  /**
   * @summary 添加新顾客
   * @description 添加新顾客
   * @Router post /customer/addCustomer
   * @request username string *username 账户名
   * */
  async addCustomer() {
    const { ctx } = this;
    try {
      const user = await ctx.service.customer.customer.addCustomer(
        ctx.request.body
      );
      this.success({ user });
    } catch (error) {
      console.log(error);
      this.error(error);
    }
  }
  /* 添加联系人 */
  async addContact() {
    const { ctx } = this;
    try {
      const contact = await ctx.service.customer.customer.addContact(
        ctx.request.body
      );
      this.success({ contact });
    } catch (error) {
      this.error(error);
    }
  }
  /* 添加追踪记录 */
  async addTrack() {
    const { ctx } = this;
    try {
      const res = await ctx.service.customer.customer.addTrack(
        ctx.request.body
      );
      this.success({ res });
    } catch (error) {
      this.error(error);
    }
  }
  /* 添加用户申请单 */
  async addExamine() {
    const { ctx } = this;
    try {
      const res = await ctx.service.customer.customer.addExamine(
        ctx.request.body
      );
      this.success({ res });
    } catch (error) {
      this.error(error);
    }
  }
  /* -----------------查----------------- */
  /* 查询客户是否撞单 */
  async checkCustomer() {
    const { ctx } = this;
    ctx.validate(
      {
        name: {
          type: 'string',
          required: true,
          allowEmpty: true,
        },
        country: {
          type: 'string',
          required: true,
          allowEmpty: true,
        },
        province: {
          type: 'string',
          required: true,
          allowEmpty: true,
        },
        city: {
          type: 'string',
          required: true,
          allowEmpty: true,
        },
      },
      ctx.query
    );
    const customer = await ctx.service.customer.customer.findCustomerByName(
      ctx.query
    );
    if (customer) {
      this.success({ customer });
    } else {
      this.error();
    }
  }
  /* 模糊查询潜在客户 */
  async getLatentCustomerList() {
    const { ctx } = this;
    ctx.validate(
      {
        email: {
          type: 'string',
          required: true,
          allowEmpty: true,
        },
        phone: {
          type: 'string',
          required: true,
          allowEmpty: true,
        },
        name: {
          type: 'string',
          required: true,
          allowEmpty: true,
        },
      },
      ctx.query
    );
    const customers = await ctx.service.customer.customer.findLatentCustomer(
      ctx.query
    );
    if (customers) {
      this.success({ customers });
    } else {
      this.error();
    }
  }

  /* -----------------改----------------- */
  /* 修改用户 */
  async editCustomer() {
    const { ctx } = this;
    const customer = await ctx.service.customer.customer.editCustomer(
      ctx.request.body
    );
    this.success(customer);
  }
  /* 修改联系人 */
  async editContact() {
    const { ctx } = this;
    const contact = await ctx.service.customer.customer.editContact(
      ctx.request.body
    );
    if (contact) {
      this.success(contact);
    } else {
      this.error(contact);
    }
  }
  /* 转移客户 */
  async transferCustomer() {
    const { ctx } = this;
    const result = await ctx.service.customer.customer.transferCustomer(
      ctx.request.body
    );
    this.success(result);
  }
  /* -----------------删----------------- */
  /* 删除联系人 */
  async deleteContact() {
    const { ctx } = this;
    try {
      const result = await ctx.service.customer.customer.deleteContact(
        ctx.query
      );
      this.success(result);
    } catch (error) {
      this.error(error);
    }
  }
  async deleteTrack() {
    const { ctx } = this;
    try {
      const result = await ctx.service.customer.customer.deleteTrack(ctx.query);
      this.success(result);
    } catch (error) {
      this.error(error);
    }
  }
}
module.exports = SysUserController;
